/**
 * @File name : EndecryptUtils.java
 * @Description : 
 * @Creator : haifeng17
 * @CreateTime : Mar 22, 2017 4:15:48 PM
 * @Version : 1.0
 * @Update records:
 *      1.1 Mar 22, 2017 by haifeng17: 
 *      1.0 Mar 22, 2017 by haifeng17: Created 
 * All rights served : XXX CORP.
 */
package com.ent.app.bms.util;
import java.security.Key;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.codec.Hex;
import org.apache.shiro.crypto.AesCipherService;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Md5Hash;

import sun.tools.tree.NullExpression;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;

public class EndecryptUtils {
    /** 
     * base64进制加密 
     * 
     * @param password 
     * @return 
     */ 
    public static String encryptBase64(String password) { 
        Preconditions.checkArgument(!Strings.isNullOrEmpty(password), "不能为空"); 
        byte[] bytes = password.getBytes(); 
        return Base64.encodeToString(bytes); 
    } 
    /** 
     * base64进制解密 
     * @param cipherText 
     * @return 
     */ 
    public static String decryptBase64(String cipherText) { 
        Preconditions.checkArgument(!Strings.isNullOrEmpty(cipherText), "消息摘要不能为空"); 
        return Base64.decodeToString(cipherText); 
    } 
    /** 
     * 16进制加密 
     * 
     * @param password 
     * @return 
     */ 
    public static String encryptHex(String password) { 
        Preconditions.checkArgument(!Strings.isNullOrEmpty(password), "不能为空"); 
        byte[] bytes = password.getBytes(); 
        return Hex.encodeToString(bytes); 
    } 
    /** 
     * 16进制解密 
     * @param cipherText 
     * @return 
     */ 
    public static String decryptHex(String cipherText) { 
        Preconditions.checkArgument(!Strings.isNullOrEmpty(cipherText), "消息摘要不能为空"); 
        return new String(Hex.decode(cipherText)); 
    } 
    public static String generateKey() 
    { 
        AesCipherService aesCipherService=new AesCipherService(); 
        Key key=aesCipherService.generateNewKey(); 
        return Base64.encodeToString(key.getEncoded()); 
    } 
    /** 
     * 对密码进行md5加密,并返回密文和salt，包含在User对象中 
     * @param username 用户名 
     * @param password 密码 
     * @return 密文和salt 
     */ 
    public static Map<String, String> encryptMD5(String username,String password){ 
        Map<String, String> map = new HashMap<String, String>();
        if(!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)){
            SecureRandomNumberGenerator secureRandomNumberGenerator=new SecureRandomNumberGenerator();
            String salt= secureRandomNumberGenerator.nextBytes().toHex(); 
            map.put("salt", salt);
            map.put("pwd", new Md5Hash(password,username+salt,2).toBase64());
        }
        return map; 
    } 
}
